package com.lbd.statistics.controller;

import com.lbd.base.controller.BaseController;
import com.lbd.commons.Result;
import com.lbd.commons.ResultState;

import com.lbd.statistics.controller.vo.PlatformStatisticsVo;
import com.lbd.statistics.service.PlatformStatisticsService;
import com.lbd.util.ExcelExportUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by sun on 2016/12/1.
 */
@RestController
@RequestMapping("/plarform")
public class PlatformStatisticsController extends BaseController {
    private final Logger log = Logger.getLogger(this.getClass());

    @Autowired(required = true)
    PlatformStatisticsService platformStatisticsService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(HttpServletRequest request, HttpServletResponse response) throws ParseException {
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        PlatformStatisticsVo vo = new PlatformStatisticsVo();
        if (startDate != null && startDate.length() > 0) {
            vo.setStartDate(DateUtils.parseDate(startDate, new String[]{"yyyy-MM-dd HH:mm:ss"}));
        }
        if (endDate != null && endDate.length() > 0) {
            vo.setEndDate(DateUtils.parseDate(endDate, new String[]{"yyyy-MM-dd HH:mm:ss"}));
        }
        return new Result(ResultState.SUCCESS, "查询成功", platformStatisticsService.findByTime(vo));

    }

    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public void export(HttpServletRequest request, HttpServletResponse response) throws ParseException {
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        PlatformStatisticsVo vo = new PlatformStatisticsVo();
        if (startDate != null && startDate.length() > 0) {
            vo.setStartDate(DateUtils.parseDate(startDate, new String[]{"yyyy-MM-dd HH:mm:ss"}));
        }
        if (endDate != null && endDate.length() > 0) {
            vo.setEndDate(DateUtils.parseDate(endDate, new String[]{"yyyy-MM-dd HH:mm:ss"}));
        }
        PlatformStatisticsVo platformStatictisVo = new PlatformStatisticsVo();
        platformStatictisVo = platformStatisticsService.findByTime(vo);
        List<PlatformStatisticsVo> list = new ArrayList<PlatformStatisticsVo>();
        list.add(platformStatictisVo);
        ExcelExportUtils.exportExcel(request, response, DateFormatUtils.format(new Date(), "yyyyMMddHHmmss"), PlatformStatisticsVo.class, list);
        log.info("文件生成...");
    }

}
